﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Analyses;
using PH.Model.Analyses.Bureaus;
using PH.Logic.Analyses;
namespace PH.Web.Management.Areas.Services.Controllers
{
    [RouteArea("Services")]
    [RoutePrefix("ByBureau")]
    public class ByBureauApiController : ServiceBase
    {
        [Route("GetB4School")]
        [HttpPost]
        public JsonEntitySet<B4SchoolModel> GetB4School(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.Bureau != null)
                {
                    BAnalysisQuery sea = new BAnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.GradeLevel = request.Value<int>("gradeLevel");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.BGuid = user.Bureau.Guid;
                    var ret = BureauLogic.GetB4School(sea);
                    return new JsonEntitySet<B4SchoolModel>(ret);
                }
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<B4SchoolModel>(null, errinfo, false);
        }

        [Route("GetB4OriSchool")]
        [HttpPost]
        public JsonEntitySet<B4OriSchoolModel> GetB4OriSchool(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.Bureau != null)
                {
                    BAnalysisQuery sea = new BAnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.GradeLevel = request.Value<int>("gradeLevel");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.BGuid = user.Bureau.Guid;
                    var ret = BureauLogic.GetB4OriSchool(sea);
                    return new JsonEntitySet<B4OriSchoolModel>(ret);
                }
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<B4OriSchoolModel>(null, errinfo, false);
        }

        [Route("GetB4Bureau")]
        [HttpPost]
        public JsonEntitySet<B4BureauModel> GetB4Bureau(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.Bureau != null)
                {
                    BAnalysisQuery sea = new BAnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.GradeLevel = request.Value<int>("gradeLevel");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.BGuid = user.Bureau.Guid;
                    var ret = BureauLogic.GetB4Bureau(sea);
                    return new JsonEntitySet<B4BureauModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<B4BureauModel>(null, errinfo, false);
        }

        [Route("GetB4SampleBureau")]
        [HttpPost]
        public JsonEntitySet<B4SampleBureauModel> GetB4SampleBureau(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.Bureau != null)
                {
                    BAnalysisQuery sea = new BAnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.BGuid = user.Bureau.Guid;
                    var ret = BureauLogic.GetB4SampleBureau(sea);
                    return new JsonEntitySet<B4SampleBureauModel>(ret);
                }
               
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<B4SampleBureauModel>(null, errinfo, false);
        }

        [Route("GetB4SampleSchool")]
        [HttpPost]
        public JsonEntitySet<B4SampleSchoolModel> GetB4SampleSchool(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.Bureau != null)
                {
                    B4SampleSchoolQuery sea = new B4SampleSchoolQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.Finish = request.Value<bool?>("finish");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.BGuid = user.Bureau.Guid;
                    var ret = BureauLogic.GetB4SampleSchool(sea);
                    return new JsonEntitySet<B4SampleSchoolModel>(ret);
                }
                
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<B4SampleSchoolModel>(null, errinfo, false);
        }

        [Route("GetB4Grade")]
        [HttpPost]
        public JsonEntitySet<B4GradeModel> GetB4Grade(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);
                if (user.Bureau != null)
                {
                    BAnalysisQuery sea = new BAnalysisQuery();
                    sea.PageIndex = request.Value<int>("pageIndex");
                    sea.PageSize = request.Value<int>("pageSize");
                    sea.Key = request.Value<string>("key");
                    sea.History = request.Value<bool>("history");
                    var aGuid = request.Value<string>("aGuid");
                    if (!string.IsNullOrEmpty(aGuid))
                        sea.ArchiveGuid = new Guid(aGuid);
                    sea.BGuid = user.Bureau.Guid;
                    var ret = BureauLogic.GetB4Grade(sea);
                    return new JsonEntitySet<B4GradeModel>(ret);
                }
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<B4GradeModel>(null, errinfo, false);
        }
    }
}
